/* 
 * SuIndexedDB
 * @since 1.0 <2015-6-23> SoChishun Added.
 */
var SuIndexedDB = {
    newInstance: function () {
        var obj = {
            config: {
                debug: true,
                db: null,
                add_fn: function () {
                },
                get_fn: function (result) {
                },
                upgrade_fn: function (e) {

                },
            },
            set_config: function (options) {
                $.extend(this.config, options);
            },
            open: function (name, version) {
                if (!window.indexedDB)
                {
                    alert("你的浏览器不支持IndexedDB,请更换为谷歌浏览器!");
                    return;
                }
                version = version || 1;
                var request = window.indexedDB.open(name, version);
                request.onerror = function (e) {
                    if (SuIndexedDB.config.debug) {
                        console.log(e.currentTarget.error.message);
                    }
                };
                request.onsuccess = function (e) {
                    SuIndexedDB.config.db = e.target.result;
                    if (SuIndexedDB.config.debug) {
                        console.log('Open Success!');
                    }
                };
                request.onupgradeneeded = function (e) {
                    this.config.upgrade_fn(e);
                    if (SuIndexedDB.config.debug) {
                        console.log('DB version changed to ' + version);
                    }
                    /*
                     var db = e.target.result;
                     if (!db.objectStoreNames.contains('students')) {
                     db.createObjectStore('students', {keyPath: "id"});
                     }*/
                };
            },
            add: function (storeName, data) {
                var transaction = this.db.transaction(storeName, 'readwrite');
                var store = transaction.objectStore(storeName);
                store.add(data);
            },
            get: function (storeName, value) {
                var transaction = this.db.transaction(storeName, 'readwrite');
                var store = transaction.objectStore(storeName);
                var request = store.get(value);
                request.onsuccess = function (e) {
                    this.config.get_fn(e.target.result);
                };
            },
            update: function (storeName, data) {
                var transaction = this.config.db.transaction(storeName, 'readwrite');
                var store = transaction.objectStore(storeName);
                var request = store.get(data);
                request.onsuccess = function (e) {
                    store.put(data);
                };
            },
            delete: function (storeName, data) {
                var transaction = this.config.db.transaction(storeName, 'readwrite');
                var store = transaction.objectStore(storeName);
                store.delete(data);
            },
            clear: function (storeName) {
                var transaction = this.db.transaction(storeName, 'readwrite');
                var store = transaction.objectStore(storeName);
                store.clear();
            },
        };
        return obj;
    }
};


